// orderDetail.js
var app = getApp()
let upload=''
import { formatData } from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_type:null,
    page:1,
    orderDetailList:[],
    inputShowed: false,
    inputVal: "",
    order_Id:null,
    stateNum:null,
    showOrder:'',
    hideOrder:'',
    windowHeight:null
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self=this
    self.setData({
      order_type: options.order_type,
      showOrder: options.order_type,
      hideOrder: options.order_type
    })
    self.orderDetailList()
    self.orderStateNum()
    upload = getCurrentPages()[0]

    wx.getSystemInfo({
      success: function (res) {
        self.setData({
          windowHeight: parseInt(res.windowHeight)-54
        })

        console.log(self.data.windowHeight)
      }
    })

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    upload.installList()
    upload.orderStateNum()
  },
  //获取详情列表
  orderDetailList: function (e){
    let self = this
    console.log(e)
    if(e!=undefined && e!=null){
       self.data.order_type=e.currentTarget.dataset.ordertype
       self.setData({
         showOrder: e.currentTarget.dataset.ordertype,
         hideOrder: ''
       })
    }
    app.query("web/orders/filter_pending_orders", { type: self.data.order_type, page: self.data.page }).then(res => {
      if (res.code === 200) {
        console.log(res)
        let formatOrders = formatData(res.orders)
        self.setData({
          orderDetailList: formatOrders
        })     
      }
    })
  },
  //订单搜索
  search:function(e){
    let self=this
    let keyword = e.detail.value
    self.setData({ inputVal: keyword })
    app.query('web/search_orders', { q: keyword, limit: 15 }).then(res => {
      if (res.code == 200) {
        let orders = formatData(res.orders)
        self.setData({
          orderDetailList: orders
        })
        console.log(res)
      }
    })
    
  },
  //订单详情状态
  orderDetailState:function(e){
    console.log(e)
    let self = this
    self.setData({
      order_Id: e.currentTarget.dataset.orderid
    })
    if (self.data.order_Id != null) {
      wx.navigateTo({ url: '/pages/orderDetailState/orderDetailState?order_Id=' + self.data.order_Id })
    }
  },
  //获取订单各状态下的数
  orderStateNum:function(){
    let self=this
    app.query("web/orders/pending_orders").then(res=>{
      if(res.code===200){
        self.setData({
          stateNum: res.options
        })
        console.log(self.data.stateNum);
      }
    })
  },
 
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
})